<?php
class TeacherAction extends Action {

    public function _initialize() {
        $Setting = M('Setting');
        $setting = $Setting->find(1);
        
        $this->assign('setting', $setting);
    }
    
    public function index() {
        $Category = M('Category');
        $cat_list =  $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
        $User = M('User');
        // 去掉无内容的分类
        foreach($cat_list as $key => $value) {
            $count = $User->where(array('cat_id' => $value['cat_id']))->count();
            if ($count == 0) {
                unset($cat_list[$key]);
            }
        }
        $this->assign('cat_list', $cat_list);
        
        // $sidebar[0] = array('name' => '教师文章', 'url' => U('Article/category?id=29'));
        // $sidebar[1] = array('name' => '教师文件下载', 'url' => U('File/index?id=30'));
        $sidebar[0] = array('name' => '教师文章', 'url' => U('Teacher/article'));
        $sidebar[1] = array('name' => '教师文件下载', 'url' => U('Teacher/file'));
        $this->assign('sidebar', $sidebar);
        
        /* 获取cat_id */
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $category[0] = $Category->where(array('cat_id' => $id))->find();
        } else {
            $category = $cat_list;
        }
        // $this->assign('category', $category);
        
        $User = M('User');
        $user = array();
        $cat = array();
        foreach ($category as $cat_value) {
            $cat_id = $cat_value['cat_id'];
            // $teacher[$cat_id][] = $cat_value;
            // echo $cat_value['cat_name'];
            $user[$cat_id] = $User->where(array('cat_id' => $cat_id))->order(array('user_id' => 'asc'))->select();
            $cat[$cat_id] = $cat_value['cat_name'];
        }
        // dump($user);
        $this->assign('user', $user);
        $this->assign('cat', $cat);
        
        $this->display();
    }
    
    public function profile() {
        $User = M('User');
        $Teacher = M('Teacher');
        
        /* 获取id */
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
            $user = $User->find($id);
            $this->assign('user', $user);
        } else {
            $this->error('非法操作！');
        }
        
        /* 获取教师信息 */
        $teacher = $Teacher->find($user['teacher_id']);
        $this->assign('teacher', $teacher);
        
        $this->display();
    }
    
    /* 教师文章 */
    public function article() {
        // 侧边栏
        $Category = M('Category');
        $cat_list =  $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
        $User = M('User');
        // 去掉无内容的分类
        foreach($cat_list as $key => $value) {
            $count = $User->where(array('cat_id' => $value['cat_id']))->count();
            if ($count == 0) {
                unset($cat_list[$key]);
            }
        }
        $this->assign('cat_list', $cat_list);
        
        // $sidebar[0] = array('name' => '教师文章', 'url' => U('Article/category?id=29'));
        // $sidebar[1] = array('name' => '教师文件下载', 'url' => U('File/index?id=30'));
        $sidebar[0] = array('name' => '教师文章', 'url' => U('Teacher/article'));
        $sidebar[1] = array('name' => '教师文件下载', 'url' => U('Teacher/file'));
        $this->assign('sidebar', $sidebar);
        
        
        // 主题内容
        $id = 29;
        $Article = M('Article');
        import("ORG.Util.Page");
        import("ORG.Util.PageNav");

        $count = $Article->where(array('cat_id' => $id))->count();
        $Page = new PageNav($count, 25);
        $Page->setConfig('theme', '<li>%upPage%</li> <li id="n_num">%nowPage%/%totalPage%</li> <li>%downPage%</li>');
        $show = $Page->show();
        $this->assign('page',$show);
        
        $article = $Article->where(array('cat_id' => $id))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $Category = M('Category');
        $category = $Category->find($id);
        
        $this->assign('article', $article);
        $this->assign('category', $category);
        
        // $other = $Article->where(array('cat_id' => $id))->order(array('article_id' => 'asc'))->limit(8)->select();
        // $this->assign('other', $other);
        $news = $Article->where(array('cat_id' => 13))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit(5)->select();
        $this->assign('news', $news);
        
        $this->display();
    }
    
    /* 教师共享文件 */
    public function file() {
        // 侧边栏
        $Category = M('Category');
        $cat_list =  $Category->where(array('cat_type_id' => 3))->order(array('cat_id' => 'asc'))->select();
        $User = M('User');
        // 去掉无内容的分类
        foreach($cat_list as $key => $value) {
            $count = $User->where(array('cat_id' => $value['cat_id']))->count();
            if ($count == 0) {
                unset($cat_list[$key]);
            }
        }
        $this->assign('cat_list', $cat_list);
        
        // $sidebar[0] = array('name' => '教师文章', 'url' => U('Article/category?id=29'));
        // $sidebar[1] = array('name' => '教师文件下载', 'url' => U('File/index?id=30'));
        $sidebar[0] = array('name' => '教师文章', 'url' => U('Teacher/article'));
        $sidebar[1] = array('name' => '教师文件下载', 'url' => U('Teacher/file'));
        $this->assign('sidebar', $sidebar);
        
        
        // 主题内容
        $File = M('File');
        import("ORG.Util.Page");
        import("ORG.Util.PageNav");
        
        /* 教师下载category id */
        $id = 30;
        $condition['econ_category.cat_id'] = $id;
        $count = $File->where(array('cat_id' => $id))->count();
        
        $Page = new PageNav($count, 25);
        $Page->setConfig('theme', '<li>%upPage%</li> <li id="n_num">%nowPage%/%totalPage%</li> <li>%downPage%</li>');
        $show = $Page->show();
        $this->assign('page',$show);
        
        $file = $File->join(array('econ_category ON econ_file.cat_id = econ_category.cat_id'))->where($condition)->order(array('econ_file.file_id' => 'desc'))->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('file', $file);
        
        // 最新新闻
        $Article = M('Article');
        $news = $Article->where(array('cat_id' => 13))->order(array('add_time' => 'desc', 'article_id' => 'desc'))->limit(5)->select();
        $this->assign('news', $news);
        
        $this->display();
    }
    
}
?>